package com.game.grid;

import javax.microedition.khronos.opengles.GL10;

import com.mcodesal.framework.ResourceProvider;
import com.mcodesal.framework.Sprite2D;
import com.mcodesal.framework.ResourceProvider.Texture;

import android.graphics.Point;

public class Box {
	
	private Point myLocation;
	private int size;
	private int WALL_WIDTH = 16;
	public boolean upBox;
	private Sprite2D square;
	private AnimatedSprite westWall;
	private AnimatedSprite eastWall;
	private AnimatedSprite northWall;
	private AnimatedSprite southWall;
	
	public Box(int x, int y, int boxSize) {  //x,y cords put in are the top left corner.
		myLocation = new Point(x, y);
		size = boxSize;
		square = new Sprite2D(boxSize, boxSize);
		square.setTexture(ResourceProvider.getInstance().getTexture(Texture.PATH));
		upBox = false;
	}
	public void initializeWestWall() {
		westWall = new AnimatedSprite(WALL_WIDTH, size, 32, 128, 6, 128, 3, 42);
		westWall.setFrameClip(0f, 0f, 6f/32f, 1f);
		westWall.setTexture(ResourceProvider.getInstance().getTexture(Texture.WALL_ANIM));
	}
	public void initializeEastWall() {
		eastWall = new AnimatedSprite(WALL_WIDTH, size, 32, 128, 6, 128, 3, 42);
		eastWall.setFrameClip(0f, 0f, 6f/32f, 1f);
		eastWall.setTexture(ResourceProvider.getInstance().getTexture(Texture.WALL_ANIM));
	}
	public void initializeNorthWall() {
		northWall = new AnimatedSprite(WALL_WIDTH, size, 32, 128, 6, 128, 3, 42);
		northWall.setFrameClip(0f, 0f, 6f/32f, 1f);
		northWall.setTexture(ResourceProvider.getInstance().getTexture(Texture.WALL_ANIM));
	}
	public void initializeSouthWall() {
		southWall = new AnimatedSprite(WALL_WIDTH, size, 32, 128, 6, 128, 3, 42);
		southWall.setFrameClip(0f, 0f, 6f/32f, 1f);
		southWall.setTexture(ResourceProvider.getInstance().getTexture(Texture.WALL_ANIM));
	}
	public void moveUp(int speed) { //function takes in the speed at which you want the boxes to move down screen
		myLocation.y += speed*4;
	}
	
	public int getLeftSide() {
		return myLocation.x;
	}
	public int getRightSide() {
		return myLocation.x + size;
	}
	public int getTopSide() {
		return myLocation.y;
	}
	public int getBottomSide() {
		return myLocation.y + size;
	}
	
	public void setY(int y) {
		myLocation.y = y;
	}
	
	public int getBoxSize() {
		return size;
	}
	public boolean getUpBoxStatus() {
		return upBox;
	}
	public void setUpBox() {
		upBox = true;
	}
	
	public void addToX(int var) {
		myLocation.x += var;
	}
	public void setWallFrameNumber(int i) {
		if(westWall != null) {
            westWall.setFrameNumber(i);
        }
        if(eastWall != null) {
            eastWall.setFrameNumber(i);
        }
        if(northWall != null) {
            northWall.setFrameNumber(i);
        }
        if(southWall != null) {
            southWall.setFrameNumber(i);
        }
	}
	public void update(long gameTime) {
		
        if(westWall != null) {
            westWall.update(gameTime);
        }
        if(eastWall != null) {
            eastWall.update(gameTime);
        }
        if(northWall != null) {
            northWall.update(gameTime);
        }
        if(southWall != null) {
            southWall.update(gameTime);
        }
	}
	public void draw(GL10 gl) {
		
		
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
        
		gl.glPushMatrix();
        gl.glTranslatef(myLocation.x, myLocation.y, 0);
        square.draw(gl);
        
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        
        if(westWall != null) {
        	gl.glPushMatrix();
            gl.glTranslatef(-WALL_WIDTH/2, 0, 0);
            westWall.draw(gl);
            gl.glPopMatrix();
        }
        if(eastWall != null) {
            gl.glPushMatrix();
            gl.glTranslatef(size-WALL_WIDTH/2, 0, 0);
            eastWall.draw(gl);
            gl.glPopMatrix();
        }
        if(northWall != null) {
            gl.glPushMatrix();
            gl.glTranslatef(0, -WALL_WIDTH/2, 0);
            
            gl.glRotatef(-90, 0, 0, 1f);
            gl.glTranslatef(-WALL_WIDTH, 0, 0);
            northWall.draw(gl);
            gl.glPopMatrix();
        }
        if(southWall != null) {
            gl.glPushMatrix();
            gl.glTranslatef(0, size-WALL_WIDTH/2, 0);
            
            gl.glRotatef(-90, 0, 0, 1f);
            gl.glTranslatef(-WALL_WIDTH, 0, 0);
            southWall.draw(gl);
            gl.glPopMatrix();
        }
        
        gl.glPopMatrix();
	}
}
